package cn.iocoder.yudao.module.eval.controller.admin.venueequipment;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

// 注释掉ExcelUtils导入
// import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
// 添加WordUtils导入
import cn.iocoder.yudao.framework.excel.core.util.WordUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.eval.controller.admin.venueequipment.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.venueequipment.VenueEquipmentDO;
import cn.iocoder.yudao.module.eval.service.venueequipment.VenueEquipmentService;

@Tag(name = "管理后台 - 场地设备")
@RestController
@RequestMapping("/eval/venue-equipment")
@Validated
public class VenueEquipmentController {

    @Resource
    private VenueEquipmentService venueEquipmentService;

    @PostMapping("/create")
    @Operation(summary = "创建场地设备")
    @PreAuthorize("@ss.hasPermission('eval:venue-equipment:create')")
    public CommonResult<Long> createVenueEquipment(@Valid @RequestBody VenueEquipmentSaveReqVO createReqVO) {
        return success(venueEquipmentService.createVenueEquipment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新场地设备")
    @PreAuthorize("@ss.hasPermission('eval:venue-equipment:update')")
    public CommonResult<Boolean> updateVenueEquipment(@Valid @RequestBody VenueEquipmentSaveReqVO updateReqVO) {
        venueEquipmentService.updateVenueEquipment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除场地设备")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:venue-equipment:delete')")
    public CommonResult<Boolean> deleteVenueEquipment(@RequestParam("id") Long id) {
        venueEquipmentService.deleteVenueEquipment(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除场地设备")
                @PreAuthorize("@ss.hasPermission('eval:venue-equipment:delete')")
    public CommonResult<Boolean> deleteVenueEquipmentList(@RequestParam("ids") List<Long> ids) {
        venueEquipmentService.deleteVenueEquipmentListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得场地设备")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('eval:venue-equipment:query')")
    public CommonResult<VenueEquipmentRespVO> getVenueEquipment(@RequestParam("id") Long id) {
        VenueEquipmentDO venueEquipment = venueEquipmentService.getVenueEquipment(id);
        return success(BeanUtils.toBean(venueEquipment, VenueEquipmentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得场地设备分页")
    @PreAuthorize("@ss.hasPermission('eval:venue-equipment:query')")
    public CommonResult<PageResult<VenueEquipmentRespVO>> getVenueEquipmentPage(@Valid VenueEquipmentPageReqVO pageReqVO) {
        PageResult<VenueEquipmentDO> pageResult = venueEquipmentService.getVenueEquipmentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VenueEquipmentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出场地设备 Word")
    @PreAuthorize("@ss.hasPermission('eval:venue-equipment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVenueEquipmentExcel(@Valid VenueEquipmentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VenueEquipmentDO> list = venueEquipmentService.getVenueEquipmentPage(pageReqVO).getList();
        // 导出 Word 文档
        WordUtils.write(response, "场地设备.docx", VenueEquipmentRespVO.class,
                        BeanUtils.toBean(list, VenueEquipmentRespVO.class));
    }

}